#include "dev/inc/dev_serial.h"
#include "dev/inc/dev_debug.h"
#include <QMessageBox>
/*
 * 扫描串口
*/
void DEV_Scan_Serial_Port(DEV_SERIAL_STR *serial)
{
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        serial->serial_name_buf << info.portName();
    }
}
/*
 * 设置串口参数
*/
void DEV_Serial_Config(DEV_SERIAL_STR *port_parameter)
{
    // 设置串口名称
    port_parameter->serial_port.setPortName(port_parameter->serial_name);
    // 设置串口波特率
    port_parameter->serial_port.setBaudRate(port_parameter->serial_baudrate);
    // 设置串口数据位
    port_parameter->serial_port.setDataBits(port_parameter->serial_databits);
    // 设置串口停止位
    port_parameter->serial_port.setStopBits(port_parameter->serial_stopbits);
    // 设置串口流控
    port_parameter->serial_port.setFlowControl(port_parameter->serial_flowcontrol);
    // 设置串口校验位
    port_parameter->serial_port.setParity(port_parameter->serial_parity);
    DEV_DEBUG_INFO("serial parameter is configure ok");
}
/*
 * 打开串口
 * 此函数调用前必须先设置串口参数
*/
int DEV_Serial_Open(DEV_SERIAL_STR *port_parameter)
{
    if(port_parameter->serial_port.isOpen() == true)
    {
        DEV_DEBUG_ERROR("serial is opened");
        return DEV_SERIAL_IS_OPENED;
    }
    else
    {
        if(port_parameter->serial_port.open(QIODevice::ReadWrite) == true)
        {
            DEV_DEBUG_INFO("serial is opened");
            return DEV_SERIAL_ERROR_NONE;
        }
        else
        {
            DEV_DEBUG_ERROR("serial open error");
            return DEV_SERIAL_OPEN_ERROR;
        }
    }
}
/*
 * 关闭串口
*/
int DEV_Serial_Close(DEV_SERIAL_STR *port_parameter)
{
    if(port_parameter->serial_port.isOpen() == true)
    {
        port_parameter->serial_port.close();
        return DEV_SERIAL_ERROR_NONE;
    }
    else
    {
        return DEV_SERIAL_IS_CLOSED;
    }
}
/*
 * 设置串口名称
*/
void DEV_Set_Serial_Name(DEV_SERIAL_STR *port_parameter, QString portname)
{
    port_parameter->serial_name = portname;
    DEV_DEBUG_INFO("set serial name is" << port_parameter->serial_name);
}
/*
*设置串口波特率
*/
void DEV_Set_Serial_Baudrate(DEV_SERIAL_STR *port_parameter, int baudrate)
{
    port_parameter->serial_baudrate = baudrate;
    DEV_DEBUG_INFO("set serial baudrate is" << port_parameter->serial_baudrate);
}
/*
 * 设置数据位
 * 默认为8
 *
*/
void DEV_Set_Serial_Databits(DEV_SERIAL_STR *port_parameter, DEV_SERIAL_DATABITS_ENUM databits)
{
    if(databits == DEV_SERIAL_DATABITS_8)
    {
        port_parameter->serial_databits = QSerialPort::Data8;
    }
    else if(databits == DEV_SERIAL_DATABITS_7)
    {
        port_parameter->serial_databits = QSerialPort::Data7;
    }
    else if(databits == DEV_SERIAL_DATABITS_6)
    {
        port_parameter->serial_databits = QSerialPort::Data6;
    }
    else if(databits == DEV_SERIAL_DATABITS_5)
    {
        port_parameter->serial_databits = QSerialPort::Data5;
    }
    else
    {
        port_parameter->serial_databits = QSerialPort::Data8;
    }
    DEV_DEBUG_INFO("set serial databits is" << port_parameter->serial_databits);
}
/*
 * 设置停止位
 * 默认为1
*/
void DEV_Set_Serial_Stopbits(DEV_SERIAL_STR *port_parameter, DEV_SERIAL_STOPBITS_ENUM stopbits)
{
    if(stopbits == DEV_SERIAL_STOPBITS_1)
    {
         port_parameter->serial_stopbits = QSerialPort::OneStop;
    }
    else if(stopbits == DEV_SERIAL_STOPBITS_1_5)
    {
        port_parameter->serial_stopbits = QSerialPort::OneAndHalfStop;
    }
    else if(stopbits == DEV_SERIAL_STOPBITS_2)
    {
        port_parameter->serial_stopbits = QSerialPort::TwoStop;
    }
    else
    {
        port_parameter->serial_stopbits = QSerialPort::OneStop;
    }
    DEV_DEBUG_INFO("set serial stopbits is" << port_parameter->serial_stopbits);
}
/*
 * 设置校验位
 * 默认无校验
*/
void DEV_Set_Serial_Parity(DEV_SERIAL_STR *port_parameter, DEV_SERIAL_PARITY_ENUM parity)
{
    if(parity == DEV_SERIAL_PARITY_N)
    {
        port_parameter->serial_parity = QSerialPort::NoParity;
    }
    else if(parity == DEV_SERIAL_PARITY_O) // 奇校验
    {
        port_parameter->serial_parity = QSerialPort::OddParity;
    }
    else if(parity == DEV_SERIAL_PARITY_E) // 偶校验
    {
        port_parameter->serial_parity = QSerialPort::EvenParity;
    }
    else
    {
        port_parameter->serial_parity = QSerialPort::NoParity;
    }
    DEV_DEBUG_INFO("set serial parity is" << port_parameter->serial_parity);
}
/*
 * 设置流量控制
 * 默认无
 * 0：无流控 1：硬件流控
*/
void DEV_Set_Serial_Flowcontrol(DEV_SERIAL_STR *port_parameter, bool flow)
{
    if(flow == false)
    {
        port_parameter->serial_flowcontrol = QSerialPort::NoFlowControl;
    }
    else if(flow == true)
    {
        port_parameter->serial_flowcontrol = QSerialPort::HardwareControl;
    }
    else
    {
        port_parameter->serial_flowcontrol = QSerialPort::NoFlowControl;
    }
    DEV_DEBUG_INFO("set serial flowcontrol is" << port_parameter->serial_flowcontrol);
}
/*设置串口显示类型*/
void DEV_Set_Serial_Display_Type(DEV_SERIAL_STR *port_parameter, DEV_SERIAL_DISPLAY_ENUM type)
{
    port_parameter->serial_display_type = type;
}
DEV_SERIAL_DISPLAY_ENUM DEV_Get_Serial_Display_Type(DEV_SERIAL_STR *port_parameter)
{
    return port_parameter->serial_display_type;
}
/*设置串口发送数据类型*/
void DEV_Set_Serial_Send_Data_Type(DEV_SERIAL_STR *port_parameter, DEV_SERIAL_DISPLAY_ENUM type)
{
    port_parameter->serial_send_data_type = type;
}
DEV_SERIAL_DISPLAY_ENUM DEV_Get_Serial_Send_Data_Type(DEV_SERIAL_STR *port_parameter)
{
    return port_parameter->serial_send_data_type;
}
/*串口发送数据*/
int DEV_Serial_Send_Data(DEV_SERIAL_STR *serial, QByteArray send_data)
{
    if(serial->serial_port.isOpen() == false)
    {
        QMessageBox::information(NULL, "错误", "串口未打开");
        return DEV_SERIAL_ERROR_SEND;
    }
    if(serial->serial_port.write(send_data) == -1)
    {
        DEV_DEBUG_ERROR("serial erite data error");
        return DEV_SERIAL_ERROR_SEND;
    }
    else
    {
        return  DEV_SERIAL_ERROR_NONE;
    }
}
/*串口接收数据*/
QByteArray DEV_Serial_Receive_Data(DEV_SERIAL_STR *serial)
{
    if(serial->serial_port.isOpen() == false)
    {
        QMessageBox::information(NULL, "错误", "串口未打开");
        return NULL;
    }
    return serial->serial_port.readAll();
}
